#include "botao.h"
#include <QStyleOptionGraphicsItem>
#include <QApplication>
#include <QDesktopWidget>
#include <QGraphicsTextItem>
#include <QGraphicsSceneMouseEvent>

Botao::Botao(QImage img_over, QImage img_out, QImage img_clicked, QString text)
    : image_over(img_over), image_out(img_out), image_clicked(img_clicked), button_text(text)
{
    over = false;
    clicked = false;

    setRect(0, 0, image_over.width(), image_over.height());
    //setFlag(ItemIsMovable);
    setAcceptHoverEvents(true);
    //float itemScale = (float) QApplication::desktop()->height() / (float)1050;
    //this->setScale(itemScale);

    QFont font;
    font.setBold(true);
    font.setPixelSize(25);
    font.setFamily("Arial");

    QGraphicsTextItem *b_text = new QGraphicsTextItem();
    b_text->setPlainText(button_text);
    b_text->setFont(font);
    b_text->setDefaultTextColor(Qt::lightGray);

    b_text->setPos(this->rect().center().x() - b_text->boundingRect().center().x(),
                   this->rect().center().y() - b_text->boundingRect().center().y());
    b_text->setParentItem(this);
}

void Botao::paint( QPainter *p, const QStyleOptionGraphicsItem *option, QWidget * )
{

    if(over && !clicked){
        p->setRenderHint(QPainter::SmoothPixmapTransform, QPainter::HighQualityAntialiasing);
        p->drawImage( option->exposedRect, image_over, option->exposedRect, Qt::OrderedAlphaDither );
    }
    else if(!over && !clicked){
        p->setRenderHint(QPainter::SmoothPixmapTransform, QPainter::HighQualityAntialiasing);
        p->drawImage( option->exposedRect, image_out, option->exposedRect, Qt::OrderedAlphaDither );
    }
    else {
        p->setRenderHint(QPainter::SmoothPixmapTransform, QPainter::HighQualityAntialiasing);
        p->drawImage( option->exposedRect, image_clicked, option->exposedRect, Qt::OrderedAlphaDither );
    }
}

void Botao::mousePressEvent(QGraphicsSceneMouseEvent *event){
    clicked = true;
    over = true;
    Botao::update();
}

void Botao::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){
    over = true;
    clicked = false;
    Botao::update();
}

void Botao::hoverEnterEvent(QGraphicsSceneHoverEvent *event){
    over = true;
    Botao::update();
}

void Botao::hoverLeaveEvent(QGraphicsSceneHoverEvent *event){
    over = false;
    clicked = false;
    Botao::update();
}
